<?php
namespace app\controllers;

use app\models\AmountRecorder;
use app\models\CateRecorder;
use app\models\DiscountRecorder;
use app\models\GoodsRecorder;
use app\models\NewRecorder;
use app\models\PurchaseRecorder;

class GoodsController extends BaseController{

    public function actionAdd(){
        $post = \Yii::$app->request->post();
        $id = intval($post['GoodsRecorder']['id']);


        if($id){
            $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->one();
            if(!$model){
                $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
                return json_encode($result);
            }
        }else{
            $model = new GoodsRecorder();
            $model->addtime = time();
        }
        $result = $model->add($post);
        return json_encode($result);
    }


    public function actionList(){
        $post = \Yii::$app->request->post('pagination');
        $page = intval($post['page'])-1;
        $pagesize = intval($post['pagesize']);
        $cate = intval($post['cate']);
        $num  = intval($post['num']);
        $price = intval($post['price']);
        $hits = intval($post['hits']);
        $sell = intval($post['sell']);
        $down = intval($post['down']);
        $time = intval($post['time']);
        $offset = $page*$pagesize;
        $list = GoodsRecorder::find();
        if($cate){
            $list=$list->andWhere('cate_id=:cate_id',[':cate_id' => $cate]);
        }
        switch($num){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('goods_num ASC');
                break;
            case 2:
                $list = $list->orderBy('goods_num DESC');
                break;
        }
        switch($price){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('goods_price ASC');
                break;
            case 2:
                $list = $list->orderBy('goods_price DESC');
                break;
        }
        switch($hits){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('hits ASC');
                break;
            case 2:
                $list = $list->orderBy('hits DESC');
                break;
        }
        switch($sell){
            case 0:
                break;
            case 1:
                $list=$list->orderBy('sell_num ASC');
                break;
            case 2:
                $list=$list->orderBy('sell_num DESC');
                break;
        }
        switch($down){
            case 0:
                break;
            case 1:
                $list = $list->andWhere('is_down=0');
                break;
            case 2:
                $list = $list->andWhere('is_down=1');
                break;
        }
        switch ($time){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('addtime DESC');
                break;
            case 2:
                $list = $list->orderBy('addtime ASC');
                break;
        }
        $count = $list->count();
        $list = $list->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];

            $cate = CateRecorder::find()->where('id='.$v['cate_id'])->one();
            $list[$k]['cate_name'] = $cate->cate_name;

            $hd_arr=[];
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->one();
                $hd_arr[]="新人购：".$model->title;
            }
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->one();
                $hd_arr[]="满件减：".$model->title;
            }
            if($v['purchase_id']){
                $model=PurchaseRecorder::find()->where('id='.$v['purchase_id'])->one();
                $hd_arr[]="限时购：".$model->title;
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->one();
                $hd_arr[]="满额减：".$model->title;
            }
            $list[$k]['hd']=$hd_arr;
        }

        $result = ['result'=>1,'data'=>$list,'count'=>$count];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }


    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }

        $model['pics'] = json_decode($model['pics'],true);
        foreach($model['pics'] as $k=>$v){
            $model['pics'][$k] = \Yii::$app->params['base_url'].$v;
        }
        $model['goods_attr'] = json_decode($model['goods_attr'],true);
        $model['specs'] = json_decode($model['specs'],true);
        $model['specs_attr'] = json_decode($model['specs_attr'],true);
        $model['specs_value'] = json_decode($model['specs_value'],true);
        foreach($model['specs_value'] as $k=>$v){
            $model['specs_value'][$k]['pic'] = \Yii::$app->params['base_url'].$v['pic'];
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return  json_encode($result);
    }


    public function actionDown(){
        $arr = \Yii::$app->request->post('arr');
        if(!is_array($arr)){
            $result = ['result'=>0,'data'=>'参数发生错误，请重新操作'];
            return json_encode($result);
        }
        $list = GoodsRecorder::find()->where(['in','id',$arr])->asArray()->all();
        foreach($list as $k=>$v){
            $model = GoodsRecorder::find()->where('id='.$v['id'])->one();
            if($model){
                $model->is_down = !$model->is_down;
                $model->save(false);
            }
        }
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionLook(){
        $id = \Yii::$app->request->post('id');
        $model = GoodsRecorder::find()->where('id=:id',[':id' => $id])->asArray()->one();
        if(empty($model)){
            $result=['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model['pics'] = json_decode($model['pics'],true);
        foreach($model['pics'] as $k=>$v){
            $model['pics'][$k] = \Yii::$app->params['base_url'].$v;
        }
        $model['goods_attr'] = json_decode($model['goods_attr'],true);

        $cate = CateRecorder::find()->where('id='.$model['cate_id'])->one();
        $model['cate_name'] = $cate->cate_name;
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionNewlist(){
        $list = NewRecorder::find()->select(['id','title'])->where('status=0')->asArray()->all();
        $result= ['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionPurchaselist(){
        $list = PurchaseRecorder::find()->select(['id','title'])->where('status=0')->asArray()->all();
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionDiscountlist(){
        $list = DiscountRecorder::find()->select(['id','title'])->where('status=0')->asArray()->all();
        $result =['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionAmountlist(){
        $list = AmountRecorder::find()->select(['id','title'])->where('status=0')->asArray()->all();
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionSellnum(){
        $id = \Yii::$app->request->post('id');
        $model = GoodsRecorder::find()->select(['id','sell_num'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSavesellnum(){
        $post = \Yii::$app->request->post('form');
        $model = GoodsRecorder::find()->where('id=:id',[':id' => $post['id']])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->sell_num+=abs(intval($post['add_sell_num']));
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionDelsl(){
        $id = \Yii::$app->request->post('id');
        if(!is_array($id)){
            $result = ['result'=>0,'data'=>'参数错误，请重新操作'];
            return json_encode($result);
        }
        foreach($id as $v){
            $model = GoodsRecorder::find()->where('id=:id',[':id' => $v])->one();
            if(!$model){
                $result = ['result'=>0,'data'=>'商品ID:'.$v."不存在，请刷新后重新操作"];
                return json_encode($result);
            }
            $model->delete();
        }
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

}